package random_test;

import java.util.ArrayList;
import java.util.List;

public class Test_10_29 {


    //1576. 替换所有的问号
    public String modifyString(String s) {
        char[] arr = s.toCharArray();
        int n = arr.length;
        for(int i = 0; i < n; i++) {
            // 替换 ? 字符
            if(arr[i] == '?') {
                for(char ch = 'a'; ch <= 'z'; ch++) {
                    if((i == 0 || ch != arr[i-1]) && (i == n-1 || ch != arr[i+1])) {
                        arr[i] = ch;
                        break;
                    }
                }
            }
        }
        return new String(arr);
    }



    //495. 提莫攻击
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int ret = 0;
        for(int left = 0, right = 1; right < timeSeries.length; left++,right++) {
            int gap = timeSeries[right] - timeSeries[left];
            if(gap >= duration) ret += duration;
            else ret += gap;
        }
        return ret += duration;
    }


    //6. N 字形变换
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;

        List<StringBuilder> lists = new ArrayList<>();
        for(int i = 0; i < numRows; i++) lists.add(new StringBuilder());
        int row = 0, flag = -1;     //row 记录当前填入字符所在的行数, flag代表 row 移动的方向
        for(char ch : s.toCharArray()) {
            if(row == 0 || row == numRows - 1) flag *= -1;
            lists.get(row).append(ch);
            row += flag;
        }

        StringBuilder ret = new StringBuilder();
        for(int i = 0; i < lists.size(); i++) ret.append(lists.get(i));
        return ret.toString();
    }

}
